package com.tyut.controller;
import com.tyut.bean.Student;
import com.tyut.service.StudentService;
import com.tyut.service.impl.StudentServiceImpl;
import com.tyut.util.QueryRunnerBasic;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


@WebServlet("/student")
public class StudentServlet extends HttpServlet {

    private StudentService studentService = new StudentServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if (method == null) {
            method = "";
        } else if (method.equals("add")) {
            add(request, response);
        } else if (method.equals("list")) {
            list(request, response);
        } else if (method.equals("delete")) {
            delete(request, response);
        } else if (method.equals("edit")) {
            preEdit(request, response);
        } else if (method.equals("update")) {
            postEdit(request, response);
        }
    }
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Student student = new Student(
                Integer.valueOf(request.getParameter("id")),
                request.getParameter("name"),
                request.getParameter("department"),
                request.getParameter("grade"),
                request.getParameter("clazz"),
                request.getParameter("sex"),
                request.getParameter("phone")
        );
        int result= studentService.add(student);
        if (result == 0){
            response.getWriter().write("添加学生失败");
        }
        response.sendRedirect("/student?method=list");
    }

    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Student> list = studentService.queryAll();
        request.setAttribute("list", list);
        request.getRequestDispatcher("/admin/studentList.jsp").forward(request, response);
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        String id = request.getParameter("id");
        int result = studentService.delete(Integer.valueOf(id));
        if (result != 0) {
            list(request, response);
        }else {
            response.getWriter().write("删除失败");
        }
    }

    public void preEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        Student student = studentService.query(Integer.valueOf(id));
        request.setAttribute("student", student);
        request.getRequestDispatcher("/admin/editStudent.jsp").forward(request, response);
    }

    public void postEdit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Student student = new Student(
                Integer.valueOf(request.getParameter("id")),
                request.getParameter("name"),
                request.getParameter("department"),
                request.getParameter("grade"),
                request.getParameter("clazz"),
                request.getParameter("sex"),
                request.getParameter("phone")
        );
        studentService.update(student);
        list(request, response);
    }
}
